package me.sealer.ssm.service.impl;

import me.sealer.ssm.constant.ErrorCodeConstant;
import me.sealer.ssm.exception.SsmException;
import me.sealer.ssm.service.SmsService;
import me.sealer.ssm.transport.HttpTransport;
import org.apache.commons.httpclient.NameValuePair;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述： $ SmsServiceImpl
 *
 * @author sealer
 * @email 1178884049@qq.com
 * @date 2019年12月03日 17时13分17秒
 */
@Service("webChineseSmsService")
public class WebChineseSmsService implements SmsService {
    private static final String SMS_USER_UID = "sealer";
    private static final String SMS_USER_KEY = "d41d8cd98f00b204e980";
    private static final String SMS_SERVER_URL = "http://utf8.api.smschinese.cn";

    @Override
    public int sendMessage(String phoneNumber, String text) {

        NameValuePair[] data = {
                new NameValuePair("Uid", SMS_USER_UID),
                new NameValuePair("Key", SMS_USER_KEY),
                new NameValuePair("smsMob", phoneNumber),
                new NameValuePair("smsText", text)};
        Map requestMap = new HashMap(8);
        requestMap.put("Uid", SMS_USER_UID);
        requestMap.put("Key", SMS_USER_KEY);
        requestMap.put("smsMob", phoneNumber);
        requestMap.put("smsText", text);

        String result;
        try {
            result = HttpTransport.post(SMS_SERVER_URL, data);
        } catch (IOException e) {
            throw new SsmException(ErrorCodeConstant.SMS_SEND_ERROR, new Object[]{phoneNumber});
        }

        System.out.println("result: " + result);
        // TODO 此处的result待处理
        return 0;
    }
}
